package game.shared;

import game.entity.AbsAnimation;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Resources {

	private static ArrayList<TextureRegion> listTextureRegions;
	private static ArrayList<Texture> listTextures;
	private static ArrayList<String> listFilePaths;
	private static ArrayList<Integer> listIDs;
	
	public static boolean LoadResource(String configFilePath)
	{
		listFilePaths = new ArrayList<String>();
		listTextures = new ArrayList<Texture>();
		listTextureRegions = new ArrayList<TextureRegion>();
		listIDs = new ArrayList<Integer>();
		
		FileHandle file = Gdx.files.internal(configFilePath);
		String strCfg = file.readString();
		String[] lines = strCfg.split("\r\n");
		for(int i=0; i<lines.length; i++)
		{
			String[] info = lines[i].split(";");
			int n = Integer.parseInt(info[0]) + i;
			String path = info[1];
			for(++i; i<=n; i++)
			{
				String[] values = lines[i].split(";");
				int id = Integer.parseInt(values[0]); 	// resource id	
				int x = Integer.parseInt(values[1]); 	//x
				int y = Integer.parseInt(values[2]); 	//y
				int w = Integer.parseInt(values[3]); 	//w
				int h = Integer.parseInt(values[4]); 	//h
				
				listIDs.add(id);
				Texture texture = getTexture(path);
				listTextureRegions.add(new TextureRegion(texture, x, y, w, h));
			}
		}
		
		return false;
	}
	
	private static Texture getTexture(String filePath)
	{
		int i=0;	
		for (; i<listFilePaths.size(); i++)
		{
			if(listFilePaths.get(i) ==filePath )
				return listTextures.get(i);
		}
		listFilePaths.add(filePath);
		listTextures.add(new Texture(filePath));
		return listTextures.get(i);
	}
	
	public static TextureRegion getTextureRegion(int id)
	{
		int i=0;	
		for (; i<listIDs.size(); i++)
			if(listIDs.get(i) == id)
				return listTextureRegions.get(i);
		return null;
	}
}
